<?php
/**
 * 频道管理控制器
 *
 *
 *
 ** @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net
 * @link       http://www.shopnc.net
 * @since      File available since Release v1.1*/

defined('InShopNC') or exit('Access Invalid!');
class channelsControl extends SystemControl{
    private $links = array(
        //array('url'=>'act=channels&op=channels','lang'=>'nc_manage'),
        //array('url'=>'act=channels&op=channels_add','lang'=>'nc_new'),
        array('url'=>'act=channels&op=channels','lang'=>'channels_index_lists'),
        array('url'=>'act=channels&op=modules','lang'=>'modules_index_lists'),
        array('url'=>'act=channels&op=channels_add','lang'=>'channels_index_add'),
        array('url'=>'act=channels&op=modules_add','lang'=>'modules_index_add'),
        //array('url'=>'act=channels&op=tag','lang'=>'goods_class_index_tag'),
    );
    public function __construct(){
        parent::__construct();
        Language::read('channels');
    }

    /**
     * 频道管理
     */
    public function channelsOp(){
        $lang   = Language::getLangContent();
        $model_channels = Model('channels');

        //列表
        $channels_list = $model_channels->getChannelsList('');

        Tpl::output('channels_list',$channels_list);
        Tpl::output('top_link',$this->sublink($this->links,'channels'));
        Tpl::showpage('channels.index');


    }

    /**
     * 商品分类添加
     */
    public function channels_addOp(){
        $lang   = Language::getLangContent();
        $model_channels = Model('channels');
        if (chksubmit()){
            $obj_validate = new Validate();
            $obj_validate->validateparam = array(
                array("input"=>$_POST["cs_name"], "require"=>"true", "message"=>'频道名称不能为空!'),
                array("input"=>$_POST["cs_view_color"], "require"=>"true", 'validator'=>'Number', "message"=>"颜色风格未选择"),
                array("input"=>$_POST["cs_status"], "require"=>"true", 'validator'=>'Number', "message"=>"启用状态设置异常"),
            );
            $error = $obj_validate->validate();
            if ($error != ''){
                showMessage($error);
            }else {
                $insert_array = array();
                $insert_array['channels_name']        = trim($_POST['cs_name']);
                $insert_array['channels_view_color']        = intval($_POST['cs_view_color']);
                $insert_array['channels_seo_keyword']      = trim($_POST['cs_seo_keyword']);
                $insert_array['channels_seo_desi']   = trim($_POST['cs_seo_desi']);
                $insert_array['channels_status']   = intval($_POST['cs_status']);
                $result = $model_channels->addChannels($insert_array);
                if ($result){
                    $this->log(L('nc_add,goods_class_index_class').'['.$_POST['cs_name'].']',1);
                    showMessage($lang['nc_common_save_succ']);
                }else {
                    $this->log(L('nc_add,goods_class_index_class').'['.$_POST['cs_name'].']',0);
                    showMessage($lang['nc_common_save_fail']);
                }
            }
        }
        Tpl::output('top_link',$this->sublink($this->links,'channels_add'));
        Tpl::showpage('channels.add');
    }

    /**
     * 编辑
     */
    public function channels_editOp(){
        $lang   = Language::getLangContent();
        $model_channels = Model('channels');

        if (chksubmit()){
            $obj_validate = new Validate();
            $obj_validate->validateparam = array(
                array("input"=>$_POST["cs_name"], "require"=>"true", "message"=>'频道名称不能为空!'),
                array("input"=>$_POST["cs_view_color"], "require"=>"true", 'validator'=>'Number', "message"=>"颜色风格未选择"),
                array("input"=>$_POST["cs_status"], "require"=>"true", 'validator'=>'Number', "message"=>"启用状态设置异常"),
            );
            $error = $obj_validate->validate();
            if ($error != ''){
                showMessage($error);
            }

            // 更新分类信息
            $where = array('channels_id' => intval($_POST['cs_id']));
            $update_array = array();
            $update_array['channels_name']        = trim($_POST['cs_name']);
            $update_array['channels_view_color']        = intval($_POST['cs_view_color']);
            $update_array['channels_seo_keyword']      = trim($_POST['cs_seo_keyword']);
            $update_array['channels_seo_desi']   = trim($_POST['cs_seo_desi']);
            $update_array['channels_status']   = intval($_POST['cs_status']);
            $result = $model_channels->editChannels($update_array, $where);
            if (!$result){
                $this->log(L('nc_edit,goods_class_index_class').'['.$_POST['cs_name'].']',0);
                showMessage('保存失败');
            }


            $this->log(L('nc_edit,goods_class_index_class').'['.$_POST['cs_name'].']',1);
            showMessage('保存成功');
        }
        //非提交状态,显示编辑模板
        $channels_array = $model_channels->getChannelsInfoById(intval($_GET['cs_id']));
        Tpl::output('channels_array',$channels_array);
        $this->links[] = array('url'=>'act=channels&op=channels_edit','lang'=>'nc_edit');
        Tpl::output('top_link',$this->sublink($this->links,'goods_class_edit'));
        Tpl::showpage('channels.edit');
    }
    /**
     * 设置频道绑定分类
     */
    public function set_channelsOp(){
        $model_channels = Model('channels');
        $model_class = Model('goods_class');
        //频道信息
        $channels_array = $model_channels->getChannelsInfoById(intval($_GET['cs_id']));
        Tpl::output('channels_array',$channels_array);
        //分类信息
        
        //父ID
        $parent_id = $_GET['gc_parent_id']?intval($_GET['gc_parent_id']):0;
        //列表
        $tmp_list = $model_class->getTreeClassList(2);
        $class_list = array();
        if (is_array($tmp_list)){
            foreach ($tmp_list as $k => $v){
                if($v['deep'] == 1){
                    $class_list[] = $v;
                    foreach($tmp_list as $k1 => $v1){
                        if($v1['gc_parent_id'] != 0 && $v['gc_id'] == $v1['gc_parent_id']){
                            $v1['gc_name']= '-'.$v1['gc_name'];
                            $class_list[] = $v1;
                        }
                    }
                }
            }
        }
        Tpl::output('class_list',$class_list);
        $this->links[] = array('url'=>'act=channels&op=set_channels','lang'=>'nc_edit');
        Tpl::output('top_link',$this->sublink($this->links,'goods_class_edit'));
        Tpl::showpage('channels.set.channels');

    }
}
